import Koa from 'koa';
import {koaBody} from 'koa-body';
import koaStatic from 'koa-static';
import {resolve} from 'path';
import cors from '@koa/cors';
import {CWD_PATH, PORT, SERVER_ADDR} from './config';
import {startCorn} from './corn';
import catchGlobalError from './middleware/error';
import tokenMiddleware from './middleware/token';
import routers from './router';

// 启动例行化任务
startCorn();

const app = new Koa();
app.use(catchGlobalError());
app.use(cors());
app.use(tokenMiddleware([/^\/(public|tmp|ping)/]));
app.use(koaStatic(resolve(CWD_PATH, 'public')));
app.use(koaBody());
app.use(routers.routes());

app.listen(PORT, () => {
    console.log(`server at ${SERVER_ADDR}`);
});
